package com.example.lawyer.databind.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableField;

import com.example.lawyer.AccountLoginActivity;
import com.example.lawyer.R;
import com.example.lawyer.request.RequestTool;
import com.google.gson.Gson;

import org.w3c.dom.Text;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.view.Gravity.CENTER;
import static com.example.lawyer.abstractlist.AppRequestUrlList.CODE_IMAGE_LOGIN;

public class AccountLoginDataBindModel {


    public ObservableField<String> account = new ObservableField<>();//账户
    public ObservableField<String> password = new ObservableField<>();//密码
    public ObservableField<String> codesstr = new ObservableField<>();//验证，


    /****
     *
     * @param view
     * @param str
     */
    @BindingAdapter("SetTextInput")
    public static void SetTextInput(final View view, String str) {
        final EditText editText = (EditText) view;
        final Context context = view.getContext();
        final View parentvie = (View) view.getParent();
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            private String p = "^((13[0-9])|(14[5-9])|(15[0-9])|(16[5,6])|(17[0-8])|(18[0-9])|(19[0-9]))[0-9]{8}$";

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 11) {

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        Drawable drawable = context.getDrawable(R.drawable.login_edittext_bag_false);
                        parentvie.setBackground(drawable);
                    }
                    return;
                }

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    Drawable drawable = context.getDrawable(R.drawable.login_edittext_bag);
                    parentvie.setBackground(drawable);
                }
                System.out.println(s);
                Pattern compile = Pattern.compile(p);
                Matcher matcher = compile.matcher(s);
                boolean matches = matcher.matches();
                System.out.println(matches);
                if (!matches) {
                    editText.setText(null);
                    return;
                }
            }
        });
    }


    //点击提交登录
    public void subtims(View view)
    {
        String ac = this.account.get();
        if(ac==null)
        {
            Toast.makeText(view.getContext(),"请输入登录账户",Toast.LENGTH_LONG).show();
            return;
        }
        String pw = this.password.get();
        if(pw==null)
        {
            Toast.makeText(view.getContext(),"请输入密码",Toast.LENGTH_LONG).show();
            return;
        }
        String s = this.codesstr.get();
        if(s==null)
        {
            showCodeInput(view.getContext());
        }


    }


    public  void showCodeInput(Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            String s = this.account.get();

            Map<String,Object> jsomap = new HashMap<>();
            jsomap.put("phone",s);
            Gson gson = new Gson();
            String datas = gson.toJson(jsomap);
            String s1 = new RequestTool().HttpPost(CODE_IMAGE_LOGIN, datas);
            System.out.println("请求结果"+s1);


            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setGravity(CENTER);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setPadding(0,10,0,0);

            int ww =  context.getResources().getDisplayMetrics().widthPixels /2+60;
//
            ImageView imageView = new ImageView(context);
            imageView.setImageDrawable(context.getDrawable(R.mipmap.happy_captcha));
            imageView.setMaxWidth(ww-30);
            linearLayout.addView(imageView);




            EditText editText = new EditText(context);
            editText.setHint("请输入图片验证码");
            editText.setWidth(ww-30);
            editText.setHeight(80);
            editText.setTextSize(12);
            editText.setGravity(CENTER);
            editText.setMaxLines(1);
            editText.setPadding(0,5,0,5);
            editText.setBackground(context.getDrawable(R.drawable.circular_bead_bag));
            editText.setText(this.codesstr.get());
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    codesstr.set(s.toString());
                }
            });

            linearLayout.addView(editText);

//
            TextView button = new TextView(context);
            button.setText("确认");
            button.setBackgroundColor(Color.WHITE);
            button.setHeight(50);
            button.setTextSize(12);
            button.setGravity(CENTER);
            button.setPadding(0,10,0,10);
            button.setTextColor(Color.WHITE);

            button.setBackground(context.getDrawable(R.drawable.top_square_bottom_fillet));
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(account.get()==null)
                    {
                        Toast.makeText(v.getContext(),"请输入账户",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if(password.get()==null)
                    {
                        Toast.makeText(v.getContext(),"请输入密码",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if(codesstr.get()==null)
                    {
                        Toast.makeText(v.getContext(),"请输入验证码",Toast.LENGTH_LONG).show();
                        return;
                    }

                }
            });



            linearLayout.addView(button);

            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setView(linearLayout,0,0,0,0);
            Window window = alertDialog.getWindow();
            window.getDecorView().setPadding(0,0,0,0);
            window.getDecorView().setBackgroundColor(Color.parseColor("#ffffff"));
            WindowManager.LayoutParams attributes = window.getAttributes();


            attributes.width = ww;
            window.setAttributes(attributes);
            window.setBackgroundDrawable(context.getDrawable(R.drawable.circular_bead_bag));


            alertDialog.show();
        }
    }

//    public TextWatcher textWatcher = new TextWatcher() {
//        @Override
//        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//        }
//
//        @Override
//        public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//        }
//
//        @Override
//        public void afterTextChanged(Editable s) {
//            int length = s.length();
//            if(length!=11)
//            {
//                //手机号错误
//
//                return;
//            }
//
//
//
//
//        }
//    };


    /***
     * 短信登录跳转
     */
    public void onShorMes(View view) {

        System.out.println("点击有效");
        Intent intent = new Intent();
        intent.setAction("ShortMesLogin");
        view.getContext().startActivity(intent);

    }


    /***
     * 忘记密码跳转
     */
    public void amnesiaPswd(View view) {

        System.out.println("点击有效");
        Intent intent = new Intent();
        intent.setAction("AmnesiaPswd");
        view.getContext().startActivity(intent);
    }


    /***
     * 账户注册跳转
     */
    public void accountPost(View view) {
        System.out.println("点击有效");
        Intent intent = new Intent();
        intent.setAction("UserPost");
        view.getContext().startActivity(intent);
    }

    /**
     * 返回
     */
    public void turnbackClick(View view) {
        System.out.println("返回有效性");
        Intent intent = new Intent();
        intent.setAction("home");
        view.getContext().startActivity(intent);
    }
}


